<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>购物车页面</title>
    <!-- 引入Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- 引入Font Awesome -->
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <!-- 引入jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<!-- 导航栏 -->
<header class="bg-white sticky top-0 z-40 transition-all duration-300">
    <div class="container mx-auto px-4">
        <div class="flex items-center justify-between py-4">
            <!-- 左上角显示当前用户 ID -->
            <div class="flex items-center">
                <span class="text-gray-700 mr-4">当前用户 ID: <span id="currentUserIdDisplay">加载中...</span></span>
                <a href="/home" class="text-2xl font-bold text-primary">GameStore</a>
            </div>

            <nav class="hidden md:flex space-x-8">
                <a href="/home" class="text-gray-700 hover:text-primary transition-colors">首页</a>
                <a href="/gamelist" class="text-gray-700 hover:text-primary transition-colors">游戏分类</a>
                <a href="#" class="text-gray-700 hover:text-primary transition-colors">新上架</a>
                <a href="#" class="text-gray-700 hover:text-primary transition-colors">特惠</a>
            </nav>

            <!-- 右上角输入框和查询按钮 -->
            <div class="flex items-center space-x-4">
                <input type="number" id="searchUserIdInput" placeholder="输入用户 ID" class="border border-gray-300 rounded-md px-2 py-1">
                <!-- 新增搜索按钮 -->
                <button id="searchButton" class="bg-primary text-white rounded-md px-4 py-1 hover:bg-primary/90 transition-colors">搜索</button>

                <a href="/shoppingcart" class="relative text-gray-700 hover:text-primary transition-colors">
                    <i class="fa fa-shopping-cart text-xl"></i>
                    <span id="cartItemCount" class="absolute -top-2 -right-2 bg-primary text-white text-xs rounded-full w-5 h-5 flex items-center justify-center">0</span>
                </a>

                <div class="relative group">
                    <button class="flex items-center text-gray-700 hover:text-primary transition-colors">
                        <i class="fa fa-user-circle text-xl mr-2"></i>
                        <span id="usernameDisplay">我的账户</span>
                        <i class="fa fa-caret-down ml-1"></i>
                    </button>

                    <div class="absolute right-0 mt-2 w-48 bg-white rounded-md shadow-lg py-1 hidden group-hover:block z-50">
                        <a href="#" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">个人资料</a>
                        <a href="#" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">我的订单</a>
                        <a href="#" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100">收藏夹</a>
                        <div class="border-t border-gray-200 my-1"></div>
                        <a href="/logout" class="block px-4 py-2 text-sm text-red-600 hover:bg-gray-100">退出登录</a>
                    </div>
                </div>

                <button id="mobile-menu-button" class="md:hidden text-gray-700 hover:text-primary transition-colors">
                    <i class="fa fa-bars text-xl"></i>
                </button>
            </div>
        </div>
    </div>

    <!-- 移动端菜单 -->
    <div id="mobile-menu" class="md:hidden hidden bg-white border-t border-gray-200">
        <div class="container mx-auto px-4 py-3 space-y-3">
            <a href="/home" class="block py-2 text-gray-700 hover:text-primary transition-colors">首页</a>
            <a href="/gamelist" class="block py-2 text-gray-700 hover:text-primary transition-colors">游戏分类</a>
            <a href="#" class="block py-2 text-gray-700 hover:text-primary transition-colors">新上架</a>
            <a href="#" class="block py-2 text-gray-700 hover:text-primary transition-colors">特惠</a>
        </div>
    </div>
</header>

<!-- 显示用户订单的区域 -->
<div class="container mx-auto px-4 py-8">
    <div id="ordersDisplay" class="max-w-2xl mx-auto bg-white rounded-xl shadow-lg p-8 transform hover:scale-[1.01] transition-all duration-300">
        <h2 class="text-2xl font-bold text-gray-800 mb-4">用户订单列表</h2>
        <table id="ordersTable" class="w-full table-auto">
            <thead>
            <tr>
                <th class="px-4 py-2">订单 ID</th>
                <th class="px-4 py-2">总价格</th>
                <th class="px-4 py-2">状态</th>
                <th class="px-4 py-2">创建时间</th>
            </tr>
            </thead>
            <tbody>
            <!-- 订单数据将在这里动态填充 -->
            </tbody>
        </table>
        <!-- 分页按钮 -->
        <div id="pagination" class="flex justify-center mt-4 space-x-2">
            <button id="prevPageButton" class="bg-primary text-white rounded-md px-4 py-1 hover:bg-primary/90 transition-colors" disabled>上一页</button>
            <button id="nextPageButton" class="bg-primary text-white rounded-md px-4 py-1 hover:bg-primary/90 transition-colors" disabled>下一页</button>
        </div>
    </div>
</div>

<!-- 页脚 -->
<footer class="bg-dark text-white py-12 mt-auto">
    <div class="container mx-auto px-4">
        <div class="grid grid-cols-1 md:grid-cols-4 gap-8">
            <div>
                <h3 class="text-lg font-bold mb-4">GameStore</h3>
                <p class="text-gray-400">发现最新、最热门的游戏，开启你的游戏之旅。</p>
            </div>
            <div>
                <h3 class="text-lg font-bold mb-4">快速链接</h3>
                <ul class="space-y-2">
                    <li><a href="/home" class="text-gray-400 hover:text-white transition-colors">首页</a></li>
                    <li><a href="/gamelist" class="text-gray-400 hover:text-white transition-colors">游戏分类</a></li>
                    <li><a href="#" class="text-gray-400 hover:text-white transition-colors">新上架</a></li>
                </ul>
            </div>
            <div>
                <h3 class="text-lg font-bold mb-4">支持</h3>
                <ul class="space-y-2">
                    <li><a href="#" class="text-gray-400 hover:text-white transition-colors">帮助中心</a></li>
                    <li><a href="#" class="text-gray-400 hover:text-white transition-colors">联系我们</a></li>
                    <li><a href="#" class="text-gray-400 hover:text-white transition-colors">常见问题</a></li>
                </ul>
            </div>
            <div>
                <h3 class="text-lg font-bold mb-4">关注我们</h3>
                <div class="flex space-x-4">
                    <a href="#" class="w-10 h-10 rounded-full bg-gray-800 flex items-center justify-center hover:bg-primary transition-colors">
                        <i class="fa fa-facebook"></i>
                    </a>
                    <a href="#" class="w-10 h-10 rounded-full bg-gray-800 flex items-center justify-center hover:bg-primary transition-colors">
                        <i class="fa fa-twitter"></i>
                    </a>
                    <a href="#" class="w-10 h-10 rounded-full bg-gray-800 flex items-center justify-center hover:bg-primary transition-colors">
                        <i class="fa fa-instagram"></i>
                    </a>
                </div>
            </div>
        </div>
        <div class="border-t border-gray-800 mt-8 pt-8 text-center text-gray-500">
            <p>&copy; 2025 GameStore. 保留所有权利。</p>
        </div>
    </div>
</footer>

<script>
    // 移动端菜单切换
    document.getElementById('mobile-menu-button').addEventListener('click', function() {
        const mobileMenu = document.getElementById('mobile-menu');
        mobileMenu.classList.toggle('hidden');
    });

    // 导航栏滚动效果
    window.addEventListener('scroll', function() {
        const header = document.querySelector('header');
        if (window.scrollY > 10) {
            header.classList.add('bg-white/95', 'backdrop-blur-sm');
            header.classList.remove('bg-white');
        } else {
            header.classList.remove('bg-white/95', 'backdrop-blur-sm');
            header.classList.add('bg-white');
        }
    });

    // 获取并显示当前用户 ID
    $(document).ready(function() {
        $.ajax({
            url: '/api/user/id',
            type: 'GET',
            dataType: 'json',
            success: function(userId) {
                if (userId > 0) {
                    $('#currentUserIdDisplay').text(userId);
                } else {
                    $('#currentUserIdDisplay').text('未登录');
                }
            },
            error: function() {
                $('#currentUserIdDisplay').text('获取失败');
            }
        });

        // 绑定搜索按钮的点击事件
        $('#searchButton').click(function() {
            const userId = $('#searchUserIdInput').val();
            if (userId) {
                fetchOrders(userId, 1); // 初始加载第一页
            }
        });

        // 绑定上一页按钮的点击事件
        $('#prevPageButton').click(function() {
            const currentPage = parseInt($(this).data('page'));
            const userId = $('#searchUserIdInput').val();
            if (userId && currentPage > 1) {
                fetchOrders(userId, currentPage - 1);
            }
        });

        // 绑定下一页按钮的点击事件
        $('#nextPageButton').click(function() {
            const currentPage = parseInt($(this).data('page'));
            const userId = $('#searchUserIdInput').val();
            if (userId) {
                fetchOrders(userId, currentPage + 1);
            }
        });
    });

    // 定义每页显示的订单数量
    const itemsPerPage = 5;

    // 从服务器获取订单数据
    function fetchOrders(userId, page) {
        const startIndex = (page - 1) * itemsPerPage;
        $.ajax({
            url: `/order/getByUserId/${userId}`,
            type: 'GET',
            dataType: 'json',
            success: function(orders) {
                const totalOrders = orders.length;
                const endIndex = Math.min(startIndex + itemsPerPage, totalOrders);
                const currentOrders = orders.slice(startIndex, endIndex);

                // 清空订单表格
                $('#ordersTable tbody').empty();

                // 填充订单数据
                currentOrders.forEach(function(order) {
                    const row = `<tr>
                            <td class="px-4 py-2">${order.id}</td>
                            <td class="px-4 py-2">${order.totalPrice}</td>
                            <td class="px-4 py-2">${order.status}</td>
                            <td class="px-4 py-2">${order.createTime}</td>
                        </tr>`;
                    $('#ordersTable tbody').append(row);
                });

                // 更新分页按钮状态
                $('#prevPageButton').data('page', page);
                $('#nextPageButton').data('page', page);
                $('#prevPageButton').prop('disabled', page === 1);
                $('#nextPageButton').prop('disabled', endIndex === totalOrders);
            },
            error: function() {
                $('#ordersTable tbody').html('<tr><td colspan="4">获取订单失败</td></tr>');
            }
        });
    }
</script>
</body>
</html>